package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.YibaopaymentService;
import com.youi.entity.Yibaopayment;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 易宝订单流水号控制层
* @author Administrator
*/
@Controller
public class YibaopaymentController  extends HttpRequestManage{

		@Autowired
		private YibaopaymentService yibaopaymentService;

		/**
		 *添加或编辑易宝订单流水号
		*/
		@ResponseBody
		@RequestMapping("addOrEditYibaopayment")
		public String addOrEditYibaopayment(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Yibaopayment yibaopayment=null;
			if (id!=null) {
				yibaopayment=yibaopaymentService.get(id);
			}else {
				yibaopayment=new Yibaopayment();
				yibaopayment.setDelStatus(DelStatus.NODEL);
				yibaopayment.setGmtCreated(new Date());
			}
			Long userId=optLong("userId",request);
			if (userId!=null){
				yibaopayment.setUserId(userId);
			}
			Long orderId=optLong("orderId",request);
			if (orderId!=null){
				yibaopayment.setOrderId(orderId);
			}
			Integer payType=optInteger("payType",request);
			if (payType!=null){
				yibaopayment.setPayType(payType);
			}
			String serialNumber=optString("serialNumber",request);
			if (serialNumber!=null){
				yibaopayment.setSerialNumber(serialNumber);
			}
			String orderNos=optString("orderNos",request);
			if (orderNos!=null){
				yibaopayment.setOrderNos(orderNos);
			}
			String paymentNo=optString("paymentNo",request);
			if (paymentNo!=null){
				yibaopayment.setPaymentNo(paymentNo);
			}
			Double payableAmount=optDouble("payableAmount",request);
			if (payableAmount!=null){
				yibaopayment.setPayableAmount(payableAmount);
			}
			Double payAmount=optDouble("payAmount",request);
			if (payAmount!=null){
				yibaopayment.setPayAmount(payAmount);
			}
			String payData=optString("payData",request);
			if (payData!=null){
				yibaopayment.setPayData(payData);
			}
			yibaopayment.setGmtModify(new Date());
			yibaopaymentService.saveOrUpdate(yibaopayment);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除易宝订单流水号
		*/
		@ResponseBody
		@RequestMapping("delYibaopaymentByIds")
		public String delYibaopaymentByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Yibaopayment> yibaopaymentList= yibaopaymentService.getByParams(params);
			if (yibaopaymentList==null||yibaopaymentList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Yibaopayment yibaopayment : yibaopaymentList) {
				yibaopayment.setDelStatus(DelStatus.DEL);
			}
			yibaopaymentService.saveOrUpdateAll(yibaopaymentList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询易宝订单流水号
		*/
		@ResponseBody
		@RequestMapping("getYibaopayment")
		public String getYibaopayment(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Yibaopayment> yibaopaymentList=yibaopaymentService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (yibaopaymentList==null||yibaopaymentList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=yibaopaymentService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, yibaopaymentList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询易宝订单流水号
		*/
		@ResponseBody
		@RequestMapping("getYibaopaymentById")
		public String getYibaopaymentById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Yibaopayment yibaopayment=yibaopaymentService.get(id);
			if (yibaopayment==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",yibaopayment);//jsonObject	json对象
		}

		/**
		 * 查询所有易宝订单流水号
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllYibaopayment")
		public String getAllYibaopayment(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Yibaopayment> yibaopaymentList= yibaopaymentService.getByParams(null);
			if (yibaopaymentList==null||yibaopaymentList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",yibaopaymentList);//jsonObjectList	json对象集合
		}
}
